﻿using System.Collections.Generic;
using System.Web.Mvc;
using RHD.Common.Web.Utility;

namespace RHD.Web.Infrastructure {
	public class BaseController : Controller {

		private SessionManager _sessionManager;
		public SessionManager SessionManager {
			get {
				if (_sessionManager == null)
					_sessionManager = new SessionManager(System.Web.HttpContext.Current.Session);
				return _sessionManager;
			}
		}

		public Principal Principal {
			get { return this.SessionManager.GetData<Principal>(SessionTypes.PRINCIPAL) ?? new Principal(); }
			set { this.SessionManager.SetData(SessionTypes.PRINCIPAL, value); }
		}

		public void AddMsgSuccess(string message) {
			this.AddMsgTempData(ViewDataKeys.SUCCESS_MESSAGE, message);
		}

		public void AddMsgError(string message) {
			this.AddMsgTempData(ViewDataKeys.ERROR_MESSAGE, message);
		}

		private void AddMsgTempData(string key, string message) {
			if (TempData[key] == null)
				TempData[key] = new List<string>();

			((IList<string>)TempData[key]).Add(message);
		}
	}
}